 package com.lancoo.znbkxx.adapter;


import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.lancoo.znbkxx.R;
import com.lancoo.znbkxx.base.AppApplication;
import com.lancoo.znbkxx.beans.MyNoteItemBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Create by TangJun on 2019/2/16
 */
public class MyNoteDeleteAdapter extends RecyclerView.Adapter<MyNoteDeleteAdapter.ViewHolder>{

    /**
     * 展示数据
     */
    private List<MyNoteItemBean> mData;
    private StringBuilder keyString;
    private ImageAdapter adapter;
    /**
     * 事件回调监听
     */
    private MyNoteDeleteAdapter.OnItemClickListener onItemClickListener;
    private OnDeleteClickLister mDeleteClickListener;

    public MyNoteDeleteAdapter(List<MyNoteItemBean> data) {
        this.mData = data;
        keyString = new StringBuilder();
    }

    public void updateData(List<MyNoteItemBean> data) {
        this.mData = data;
        notifyDataSetChanged();
    }
    public void setKeyString(String key) {
        if (TextUtils.isEmpty(keyString.toString())) {
            keyString.append(key);
        } else {
            keyString.setLength(0);
            keyString.append(key);
        }
    }
    /**
     * 添加新的Item
     */
    public void addNewItem() {
        if(mData == null) {
            mData = new ArrayList<>();
        }
        //mData.add(0, "new Item");
        notifyItemInserted(0);
    }

    /**
     * 删除Item
     */
    public void deleteItem(int position) {
        if(mData == null || mData.isEmpty()) {
            return;
        }
        mData.remove(position);
        notifyItemRemoved(position);
    }

    /**
     * 设置回调监听
     *
     * @param listener
     */
    public void setOnItemClickListener(MyNoteDeleteAdapter.OnItemClickListener listener) {
        this.onItemClickListener = listener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        // 实例化展示的view
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_note, parent, false);
        // 实例化viewholder
        MyNoteDeleteAdapter.ViewHolder viewHolder = new MyNoteDeleteAdapter.ViewHolder(v);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        holder.tvName.setText(mData.get(position).getTitle());
        holder.tvContent.setText(mData.get(position).getContent());
        holder.tvTime.setText(mData.get(position).getUpdateTime());
        if (mData.get(position).getImagePath() != null){
            List<String> imagePath = new ArrayList<>();
            String path = mData.get(position).getImagePath();
            String[] image = path.split("\\|");
            for (String string : image){
                imagePath.add(string);
            }
            holder.rvImage.setVisibility(View.VISIBLE);
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(AppApplication.getInstance());
            linearLayoutManager.setOrientation(LinearLayout.HORIZONTAL);
            holder.rvImage.setLayoutManager(linearLayoutManager);
            adapter = new ImageAdapter(imagePath);
            holder.rvImage.setAdapter(adapter);
        }
        if (!holder.tvDelete.hasOnClickListeners()) {
            holder.tvDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mDeleteClickListener != null) {
                        mDeleteClickListener.onDeleteClick(v, position);
                    }
                }
            });
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {
                if(onItemClickListener != null) {
                    int pos = holder.getLayoutPosition();
                    onItemClickListener.onItemClick(holder.itemView, pos);
                }
            }
        });

        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if(onItemClickListener != null) {
                    int pos = holder.getLayoutPosition();
                    onItemClickListener.onItemLongClick(holder.itemView, pos);
                }
                //表示此事件已经消费，不会触发单击事件
                return true;
            }
        });
    }

    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvName;
        TextView tvContent;
        TextView tvTime;
        TextView tvDelete;
        RecyclerView rvImage;
        public ViewHolder(View itemView) {
            super(itemView);
            tvName = itemView.findViewById(R.id.tv_title);
            tvContent = itemView.findViewById(R.id.tv_content);
            tvTime = itemView.findViewById(R.id.tv_time);
            tvDelete = itemView.findViewById(R.id.tv_delete);
            rvImage = itemView.findViewById(R.id.rv_image);
        }
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
        void onItemLongClick(View view, int position);
    }
    // protected abstract void fillItemData(WrongItemAdapter.ViewHolder viewHolder, int position);

    public void setOnDeleteClickListener(OnDeleteClickLister listener) {
        this.mDeleteClickListener = listener;
    }

    public interface OnDeleteClickLister {
        void onDeleteClick(View view, int position);
    }
}